<?php
/*
	我的下线
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page_setting');
	$levels=array();
	$agent_ids=$_SESSION['uid'];
    $return['anum']=count($this->model('member')->getteamids($_SESSION['uid'],1,true,3));
    $return['bnum']=count($this->model('member')->getteamids($_SESSION['uid'],2,true,3));
	$return['jnum']=pdo_count('jy_baojfee_member',array('jie_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
    $return['allagentnum']=$return['bnum']+$return['anum'];
	$return['member']=$this->get('member',array('mid'=>$_SESSION['uid']));
	$return['params']=array('type'=>1);
	$return['psize']=20;
	$return['list']=array();
	set_html('title',$return['setting']['tmydown']?:'我的团队');
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $type=$_RQ['type'];
	if($type==1){
        $agent_ids=$this->model('member')->getteamids($_SESSION['uid'],1,true,3);
    }elseif($type==2){
        $agent_ids=$this->model('member')->getteamids($_SESSION['uid'],2,true,3);
    }elseif($type==3){
        $params['jie_id']=$_SESSION['uid'];
    }
	if(!$agent_ids && $type!=3){
		exi('none');
	}
	if($agent_ids){
		$params['mid']=$agent_ids;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'mid desc',
	);
	$list=$this->get_list('member',$parameter);
	if(!$list){
		exi('none');
	}
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		if(!$item['member']['id']){
			pdo_delete('jy_baojfee_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));	
		}
		$item['agenttime']=date('Y-m-d H:i:s',$item['agenttime']);
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['member_num']=$this->count('member',array('agent_id'=>$item['mid']),array('mid'),'mid');
		$item['leveltitle']=$this->getcolumn('level',array('id'=>$item['level_id']),'title');
		$item['leveltitle']=$item['leveltitle']?:'默认等级';
		if($item['agent_id']==$_SESSION['uid']){
            $item['isztui']=1;
        }
	}
	exi($list);
}
if($_RQ['op']=='change'){
    $post=get_postdata();
    $mid=intval($_RQ['mid']);
    $member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
    $zutype=$member['zutype']!=2?2:1;
    $this->update('member',array('zutype'=>$zutype),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
    exi('分组成功！');
}
if($_RQ['op']=='changejie'){
    $post=get_postdata();
    $jie_id=intval($post['jie_id']);
	$mid=intval($post['mid']);
	$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	if($jie_id==$member['agent_id']){
		 exi('不能设置直推为【接点人】！','error');
	}
	if($jie_id==$mid){
		 exi('不能设置自己为【接点人】！','error');
	}
	$jie=$this->get('member',array('mid'=>$jie_id,'uniacid'=>$_SESSION['uniacid']));
	if(!$jie){
		 exi('该用户不存在！','error');
	}
	if($member['isjie'] && $member['jie_id']){
		 exi('该用户已存在接点人！','error');
	}
    $this->update('member',array('jie_id'=>$jie_id,'isjie'=>1),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
    exi('设置成功！');
}
?>